10.1 A case for pie charts
Figure 10.1
### Figure 10.1 - data
bundestag <- read.csv("./data/bundestag.csv")
bundestag$party
[1] "CDU/CSU" "SPD" "FDP"
# Basic piechart
ggplot(bundestag, aes(x="", y=seats, fill=party)) +
geom_bar(stat="identity", width=1) +
coord_polar("y") +
theme_void()

### 시작: 12시, 시계 방향, 색상
bund_pie <- bundestag %>%
arrange(seats) %>%
mutate(party_fac = factor(party, levels = party[order(seats)]),
value = seats,
ypos = sum(value) - (cumsum(value)- 0.5*value),
mid_angle = 2*pi*(ypos/sum(value)),
hjust = ifelse(mid_angle<pi, 1, 0),
vjust = ifelse(mid_angle<pi/2 | mid_angle>3*pi/2, 0, 1))
### 시작: 12시, 시계 방향, 색상
bund_pie <- bundestag %>%
arrange(seats) %>%
mutate(party_fac = factor(party, levels = party[order(seats)]),
value = seats,
ypos = sum(value) - (cumsum(value)- 0.5*value),
mid_angle = 2*pi*(ypos/sum(value)),
hjust = ifelse(mid_angle<pi, 1, 0),
vjust = ifelse(mid_angle<pi, mid_angle/pi, 2-mid_angle/pi))
### Figure 10.1
ggplot(bund_pie, aes(x="", y=seats, fill=party_fac)) +
geom_bar(stat="identity") +
geom_text(aes(x = 1, y = ypos, label = seats), color = "white", size=5) +
geom_text(aes(x = 1.5, y = ypos, label = party, hjust = hjust, vjust = vjust),
color = "black", size=5) +
coord_polar(theta = "y", start = 0, direction = -1, clip = "off") +
scale_fill_manual(values = bund_pie$colors) +
theme_void() +
theme(legend.position = "none")

### Figure 10.1 - label + %
bund_pie <- bundestag %>%
arrange(seats) %>%
mutate(party_fac = factor(party, levels = party[order(seats)]),
value = seats,
pct = value/sum(value)*100,
labs = paste(party, "\n", round(pct, 0), "%", sep=""),
ypos = sum(value) - (cumsum(value)- 0.5*value))
ggplot(bund_pie, aes(x="", y=seats, fill=party_fac)) +
geom_bar(stat="identity", color="white") +
geom_text(aes(x = 1, y = ypos, label = labs), size=4) +
coord_polar(theta = "y", start = 0, direction = -1, clip = "off") +
scale_fill_brewer(palette="Set2") +
theme_void() +
theme(legend.position = "none")

10.2 A case for side-by-side bars
10.3 A case for stacked bars and stacked densities
### Figure 10.7 - data
women_parliaments <- read.csv("./data/women_parliaments.csv")
women_parliaments %>% sapply(class)
country country_code year perc_women
"character" "character" "integer" "numeric"
women_rwanda <- women_parliaments %>%
filter(country_code == "RWA" & year > 1990) %>%
mutate(women = perc_women, men = 100 - perc_women) %>%
select(-perc_women) %>%
tidyr::gather(key="gender", value="percent", women, men) %>%
mutate(gender = factor(gender, levels = c("women", "men")))
### Figure 10.7
plot_base <- ggplot(women_rwanda, aes(x = year, y = percent, fill = gender)) +
geom_col(position = "stack", width = 1,
color = "#FFFFFF", size = .75, alpha = 0.8) +
geom_hline(yintercept = 50, color = "#000000FF",
linewidth = 0.4, linetype = 2) +
geom_hline(yintercept = 100, color = "black") +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(
name = "relative proportion",
labels = scales::percent_format(scale = 1),
expand = c(0, 0)
) +
scale_fill_manual(values = c("#D55E00E0", "#0072B2E0"), guide = "none") +
theme_classic() +
theme(
axis.line.y = element_blank()
)
plot_base

### calculate label position
labels <- filter(women_rwanda, year == max(year)) %>%
mutate(pos = 100 - cumsum(percent) + 0.5*percent)
yax <- cowplot::axis_canvas(plot_base, axis = "y") +
geom_text(data = labels, aes(y = pos, label = paste0(" ", gender)),
x = 0, hjust = 0, size = 14/.pt)
cowplot::ggdraw(cowplot::insert_yaxis_grob(plot_base, yax, grid::unit(.15, "null")))

10.4 Visualizing proportions separately as parts of the total
Figure 10.12
### Figure 10.12
df_marital2 <- rbind(
mutate(df_marital,
marital = as.character(fct_collapse(marital, `never married` = "never married", aother = c("married", "divorced", "widowed"))),
highlight = "never married"
),
mutate(df_marital,
marital = as.character(fct_collapse(marital, married = "married", aother = c("never married", "divorced", "widowed"))),
highlight = "married"
),
mutate(df_marital,
marital = as.character(fct_collapse(marital, divorced = "divorced", aother = c("never married", "married", "widowed"))),
highlight = "divorced"
),
mutate(df_marital,
marital = as.character(fct_collapse(marital, widowed = "widowed", aother = c("never married", "married", "divorced"))),
highlight = "widowed"
)
) %>%
mutate(
highlight = factor(highlight, levels = c("never married", "married", "divorced", "widowed"))
)
ggplot(df_marital2, aes(age)) +
annotate(geom = "rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, fill = "#b3b3b3a0", color = NA) +
geom_density(
aes(y = after_stat(count), fill = marital), color = "transparent", position = "fill"
) +
facet_wrap(~highlight, nrow = 1) +
scale_x_continuous(
name = "age (years)",
expand = c(0, 0)
) +
scale_y_continuous(name = "relative proportion", expand = c(0, 0), labels = scales::percent) +
scale_fill_manual(
values = c("transparent", "#2b8cbed0", "#2b8cbed0", "#2b8cbed0", "#2b8cbed0"),
name = NULL,
guide = NULL
) +
coord_cartesian(clip = "off") +
theme_classic() +
theme(
panel.grid.major.y = element_line(colour = "#b3b3b3"),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
strip.background = element_blank(),
strip.text = element_text(size = 14),
legend.position = "bottom",
legend.justification = "right"
)

NA
NA